package com.gzy.studentsociety.dao;

import com.alibaba.druid.pool.DruidDataSource;
import com.gzy.studentsociety.entity.Student;

import javax.sql.DataSource; // 修正这里
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @version: java version 1.8
 * @Author: Chuan Liu
 * @description:
 * @date: 2024-10-28 17:31
 */
public abstract class BaseDao {
    private static DruidDataSource dataSource = new DruidDataSource();
    static {
        try {
            dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
            dataSource.setUrl("jdbc:mysql://localhost:3306/student_society?" +
                    "useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC");
            dataSource.setUsername("root");
            dataSource.setPassword("123456");
            // 其他设置...

            System.out.println("Druid DataSource initialized successfully.");
        } catch (Exception e) {
            System.err.println("Error initializing Druid DataSource: " + e.getMessage());
            e.printStackTrace();
        }
    }

    protected DataSource getDataSource(){
        try (Connection conn = dataSource.getConnection()) {
            System.out.println("Connection successful!");
        } catch (SQLException e) {
            System.err.println("Connection failed: " + e.getMessage());
        }
        return dataSource;
    }

//    public static void main(String[] args) {
//        BaseDao dao = new BaseDao();
//        System.out.println(dao.getDataSource());
//    }

    public abstract List<Student> queryStudent(String name, String number) throws SQLException;
}
